package com.householditemtwo.admin.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.householditemtwo.common.entity.Product;
import com.householditemtwo.common.entity.Producttype;
import com.householditemtwo.common.utils.DBManager;
import com.householditemtwo.common.utils.QueryDataResult;
import com.jspsmart.upload.Request;


/**
 * 用户DAO
 * @author admin
 *
 */
	public class ProducttypeDao {
			
		/**
		 * 查询所有产品的总条数(分页)
		 * @return
		 */
			public int count(String ptename) {
				DBManager db=new DBManager();
				String sql="select count(*) from Producttype ";
				if (ptename !=null && ptename !="" ) {
					sql+="where ptename like '%"+ptename+"%'";
				}
				QueryDataResult qdr=db.executeQuery(sql);
			int total=0;
				try {
					while(qdr.rs.next()) {
					total=qdr.rs.getInt(1);
					}
				}catch(SQLException e) {
					e.printStackTrace();
				}
				//返回查询的结果
				return total;
			}
			
		/**
		 * 查询所有产品
		 * @return
		 */
			public List<Producttype> selectAllProducttype( int page, int pageSize,String ptename) {
				DBManager db=new DBManager();
				String sql="select * from Producttype ";
						
					if (ptename !=null && ptename !="" ) {
							sql  +=" where ptename like '%"+ptename+"%'";
					}
					sql+= " order by pteid limit "+pageSize*(page-1)+","+pageSize;
				QueryDataResult qdr=db.executeQuery(sql);
				Producttype emp=new Producttype();
				ArrayList<Producttype> list=new ArrayList<Producttype>();
				try {
					while(qdr.rs.next()) {
						Producttype c=new Producttype();
						c.setPteid(qdr.rs.getInt(1));
						c.setPtename(qdr.rs.getString(2));
						
						list.add(c);
					}
				}catch(SQLException e) {
					e.printStackTrace();
				}
				//返回查询的结果
				return list;
			}
			
			
			/**
			 * 修改产品
			 * @return
			 */
		
		public int update(Producttype p) {
				//写sql
			String sql= String.format("update producttype set ptename='%s' where pteid=%d",
					p.getPtename(),p.getPteid());
					
				//执行sql
				return	DBManager.executeUpdate(sql);
			
			
		}
		/**
		 * 新增产品
		 * @return
		 */

		public int insert(Producttype p) {
			//拼接sql
			String sql= String.format("insert into producttype  values (pteid,'%s' )",p.getPtename());
					System.out.println(sql);
			//执行sql
		return	DBManager.executeUpdate(sql);
			
			
		}
	
		/**
		 * 删除产品
		 * @return
		 */
			public int delete(int id) {
				//拼接sql
				String sql= "delete from producttype where pteid="+id;
						
				//执行sql
			return	DBManager.executeUpdate(sql);
			}
	
}
